package com.chengcang.api.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chengcang.api.modules.login.utils.UserHolder;
import com.chengcang.api.modules.sys.dao.SysTenantBindingsDao;
import com.chengcang.api.modules.sys.dao.SysTenantDao;
import com.chengcang.api.modules.sys.dto.SysTenantDTO;
import com.chengcang.api.modules.sys.entity.SysTenantBindingsEntity;
import com.chengcang.api.modules.sys.entity.SysTenantEntity;
import com.chengcang.api.modules.sys.entity.UserTenant;
import com.chengcang.api.modules.sys.service.SysTenantService;
import com.chengcang.api.modules.sys.service.SysUserTenantService;
import com.chengcang.api.modules.sys.vo.SysTenantVO;
import com.chengcang.common.service.impl.CrudServiceImpl;
import com.chengcang.common.utils.ConvertUtils;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * 客户管理
 *
 * @author zhanli
 * @since 1.0.0 2023-01-05
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysTenantServiceImpl extends CrudServiceImpl<SysTenantDao, SysTenantEntity, SysTenantDTO> implements SysTenantService {

    private final SysUserTenantService sysUserTenantService;
    private final SysTenantBindingsDao sysTenantBindingsDao;

    @Override
    public QueryWrapper<SysTenantEntity> getWrapper(Map<String, Object> params) {
        return null;
    }


    @Override
    public List<SysTenantEntity> selectByIds(List<Long> tenantIds) {
        return baseDao.selectBatchIds(tenantIds);
    }

    @Override
    public List<SysTenantEntity> getListByUserId(Long userId) {
        List<UserTenant> userTenantList = sysUserTenantService.getUserTenantList(userId);
        if (!CollectionUtils.isEmpty(userTenantList)) {
            List<Long> tenantIds = userTenantList.stream().map(UserTenant::getTenantId).collect(Collectors.toList());
            return baseDao.selectBatchIds(tenantIds);
        }
        return Lists.newArrayList();
    }

    @Override
    public List<SysTenantVO> getTenantByTenantName(String name) {
        Long tenantId = UserHolder.getUser().getDefaultTenantId();
        LambdaQueryWrapper<SysTenantBindingsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<SysTenantBindingsEntity>()
                .eq(tenantId != null, SysTenantBindingsEntity::getTenantId, tenantId);
        List<SysTenantBindingsEntity> sysTenantBindingsEntities = sysTenantBindingsDao.selectList(lambdaQueryWrapper);
        if(CollectionUtils.isEmpty(sysTenantBindingsEntities)){
            return new ArrayList<>();
        }
        List<Long> bindings = sysTenantBindingsEntities.stream().map(SysTenantBindingsEntity::getBindingTenantId)
                .filter(Objects::nonNull).collect(toList());
        LambdaQueryWrapper<SysTenantEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysTenantEntity::getId, bindings).like(name != null, SysTenantEntity::getName, name);
        List<SysTenantEntity> sysTenantEntityList = baseDao.selectList(queryWrapper);

        //转换
        List<SysTenantVO> sysTenantDTOS = ConvertUtils.sourceToTarget(sysTenantEntityList, SysTenantVO.class);

        return sysTenantDTOS;
    }
}
